package com.jelly.common.util.web;

import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;
import java.util.Map;

/**
 * <h4>功能:【判断是否为ajax请求工具类】【创建人：QIAOLIANG】</h4>
 *
 * @时间:2018/6/8 0008 14:03
 * @备注:<h4></h4>
 */
public class WebUtilsPro {


    /**
     * 是否是Ajax请求
     *
     * @param request
     * @return
     * @author SHANHY
     * @create 2017年4月4日
     */
    public static boolean isAjaxRequest(HttpServletRequest request) {
        String requestedWith = request.getHeader("x-requested-with");
        if (requestedWith != null && requestedWith.equalsIgnoreCase("XMLHttpRequest")) {
            return true;
        } else {
            return false;
        }
    }


    /**
     * response 输出JSON
     *
     * @param hresponse
     * @param resultMap
     * @throws IOException
     */
    public static void out(ServletResponse response, Map<String, Object> resultMap) {

        PrintWriter out = null;
        try {
            response.setContentType("text/json;charset=utf-8");
            response.setCharacterEncoding("UTF-8");
            out = response.getWriter();
            out.println(JsonUtil.objectToJson(resultMap));

        } catch (Exception e) {
        } finally {
            if (null != out) {
                out.flush();
                out.close();
            }
        }
    }

}